package com.example.demo.netty.websocket;

import com.example.demo.netty.websocket.proto.ProtobufInfo;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

// 直接给泛型
public class PBServerHandler extends SimpleChannelInboundHandler<ProtobufInfo.ClassInfo> {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(333);

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(444);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ProtobufInfo.ClassInfo classInfo) throws Exception {
        System.out.println(123);
        // 通过getDataEnum可以得到传来的是哪个类对象，用枚举类接就可以了
        ProtobufInfo.ClassInfo.ClassType dataEnum = classInfo.getDataEnum();
        // 判断一下是谁，直接用枚举类中的和传来的比较地址即可
        if(dataEnum == ProtobufInfo.ClassInfo.ClassType.User){
            // 然后就正常调它提供的get、set方法就行了
            ProtobufInfo.User user = classInfo.getUser();
            System.out.println(user.getName()+"++++"+user.getAge());
        } else if (dataEnum == ProtobufInfo.ClassInfo.ClassType.Game) {
            ProtobufInfo.Game game = classInfo.getGame();
            System.out.println(game.getPrice()+"++++"+game.getName());
        }
//        channelHandlerContext.writeAndFlush(ByteBufAllocator.DEFAULT.buffer().writeBytes("收到".getBytes()));
//        channelHandlerContext.writeAndFlush("收到");
    }

}
